package com.swu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swu.common.Result;
import com.swu.dto.RuleAddDTO;
import com.swu.dto.RuleQueryDTO;
import com.swu.dto.RuleUpdateDTO;
import com.swu.po.ValidationRule;
import com.swu.service.IValidationRuleService;
import com.swu.vo.ValidationRuleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "校验规则管理", description = "校验规则的增删改查接口")
@RestController
@RequestMapping("/validation-rules")
@RequiredArgsConstructor
public class ValidationRuleController {

    private final IValidationRuleService validationRuleService;

    @Operation(summary = "添加规则", description = "添加新的校验规则")
    @PostMapping
    public Result<ValidationRuleVO> addRule(@RequestBody @Valid RuleAddDTO ruleAddDTO) {
        return validationRuleService.addRule(ruleAddDTO);
    }

    @Operation(summary = "更新规则", description = "更新已有的校验规则")
    @PutMapping
    public Result<ValidationRuleVO> updateRule(@RequestBody @Valid RuleUpdateDTO ruleUpdateDTO) {
        return validationRuleService.updateRule(ruleUpdateDTO);
    }

    @Operation(summary = "删除规则", description = "删除指定的校验规则")
    @DeleteMapping("/{id}")
    public Result<Void> deleteRule(
            @Parameter(description = "规则ID", required = true) @PathVariable Long id) {
        return validationRuleService.deleteRule(id);
    }

    @Operation(summary = "获取规则详情", description = "获取指定规则的详细信息")
    @GetMapping("/{id}")
    public Result<ValidationRule> getRule(
            @Parameter(description = "规则ID", required = true) @PathVariable Long id) {
        return validationRuleService.getRule(id);
    }

    @Operation(summary = "分页查询规则", description = "根据条件分页查询规则列表")
    @PostMapping("/page")
    public Result<Page<ValidationRuleVO>> listRules(@RequestBody @Valid RuleQueryDTO queryDTO) {
        return validationRuleService.listRules(queryDTO);
    }
} 